<?php

class QSobjectType
{
	/**
	 * The id of the object type as in the repository
	 *
	 * @var integer
	 */
	private $id;
	/**
	 * The name of the object type
	 *
	 * @var string
	 */
	private $name;
	/**
	 * If this type is a scalar type
	 *
	 * @var boolean
	 */
	private $is_scalar;
	/**
	 * If the properties of the objects are fixed, if true you can not add more properties than the defined ones
	 *
	 * @var boolean
	 */
	private $fixed_properties;
	/**
	 * The parent of the object type or null if none
	 *
	 * @var QSobjectType
	 */
	private $parent;
	/**
	 * The sql type to use in the repository
	 *
	 * @var string
	 */
	private $sql_type;
	/**
	 * The length of the sql type to use in the repository
	 *
	 * @var string
	 */
	private $sql_size;
	/**
	 * Only usable for scalar types. If true it will add an index to the table that contains the value
	 *
	 * @var boolean
	 */
	private $index;
	/**
	 * If the object type is embedded it means it can not be modified or removed from the repository
	 *
	 * @var boolean
	 */
	private $embedded;
	/**
	 * The properties of the object type
	 *
	 * @var array
	 */
	private $properties;
	/**
	 * In case of an enum or set , the list of values that are accepted as a key => value pair
	 * The key is the ID of the value 
	 * Only the ID will be stored in the database
	 *
	 * @var array
	 */
	private $enum_values;
	/**
	 * The repository that contains this type
	 *
	 * @var QSrepository
	 */
	private $repository;
	/**
	 * The repository table that contains this object type's data
	 *
	 * @var string
	 */
	private $repository_table;
	
	public function __construct(QSrepository $repository, $type_row)
	{
		$this->repository = $repository;
		
		// id 	name 	scalar 	fixed_props 	id_parent 	sql_type 	sql_size 	embedded 	index
		$this->id = $type_row["id"];
		$this->name = $type_row["name"];
		$this->is_scalar = $type_row["scalar"];
		$this->fixed_properties = $type_row["fixed_props"];
		
		$this->parent = $this->repository->getObjectTypeById($type_row["id_parent"]);
		$this->sql_type = $type_row["sql_type"];
		$this->sql_size = $type_row["sql_size"];
		
		$this->embedded = $type_row["embedded"];
		$this->index = $type_row["index"];
		
		if ($this->is_scalar)
			$this->repository_table = $repository->getTablesPrefix() . "w" . ucfirst( strtolower( $this->sql_type ) ) . (isset($this->sql_size) ? $this->sql_size : "") ;
		else 
			$this->repository_table = $repository->getTablesPrefix() . "objects";
	}
	
	public function addPropertyType(QSobjectPropertyType $property)
	{
		if (!isset($this->properties))
			$this->properties = array();
		$this->properties[] = $property;
	}
	
	public function setEnumAcceptedValue($row_enum)
	{
		if (!isset($this->enum_values))
			$this->enum_values = array();
		$this->enum_values[$row_enum["id"]] = $row_enum["value"];
	}
	
	public function isScalar()
	{
		return $this->is_scalar;
	}
	
	public function getRepositoryTable()
	{
		return $this->repository_table;
	}
	
	public function getSqlType()
	{
		return $this->sql_type;
	}
	
	public function getSqlSize()
	{
		return $this->sql_size;
	}
	
	public function getSqlIndex()
	{
		return $this->index;
	}
}

?>